using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Rocuna.Core;
using Rocuna.Core.Exceptions;

namespace Rocuna.GameEngine.State.Screens
{
    /// <summary>
    /// Base class for splash screens. splash screens should be drawen only one time.
    /// </summary>
    public abstract class SplashScreen : Screen
    {
        #region Constractor

        /// <summary>
        /// Initializes a new instance of the <see cref="SplashScreen"/> class.
        /// </summary>
        /// <param name="screenBackground">The screen background.</param>
        /// <param name="game">The game.</param>
        protected SplashScreen(Texture2D screenBackground, Game game)
            : base(game)
        {
            Background = screenBackground;
            DrawOrder = int.MaxValue;
        }

        #endregion

        private bool _contentLoaded = false;

        protected Texture2D Background { get; set; }

        #region Methods

        public override void Update(GameTime gameTime)
        {
            if (!_contentLoaded)
            {
                LoadAdditionalContnet(gameTime);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            var spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            if (spriteBatch == null)
                throw new NotInitializedException();
            spriteBatch.Begin();
            spriteBatch.Draw(Background, Game.GraphicsDevice.Viewport.Bounds, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void LoadAdditionalContnet(GameTime gameTime)
        {
            _contentLoaded = true;
            (Game as RocunaGame).ForceDraw(gameTime);
            LoadAllContent();
        }

        protected abstract void LoadAllContent();

        #endregion
    }
}